<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<?php 
	$USER = getAccountUserObject();
	
	$listAllThreads = $this->escort_message_thread_entity->listAllThreadMessages($USER->getId(),USER_TYPE_AGENT);	 
	$countUnread = $this->escort_message_thread_entity->countAllUnreadMessagesOfUser($USER->getId(),USER_TYPE_AGENT);	
?>
<style>
	.thread-item{
		background:#555555;
		color:#fcfcfc;
		padding:0px 0px;
		margin-bottom:2px;
		position:relative;
		cursor:pointer;
	}
	
	.thread-item:hover{
		background:#333333;
	}
	
	.read{
		background:#000;
	}
	
	.item-message{
		color:#cccccc;
		margin-left:5px;
		font-size:12px;
		width:300px;
		margin-bottom:10px;
	}
	
	.item-name{
		color:#ffffff;
		font-weight:bold;
		margin-left:5px;
		margin-bottom:5px;
		margin-top:10px;
	}
	
	.item-info-right{
		position:absolute;
		right:0px;top:20px;
		width:150px;height:30px;
		font-size:11px;
		color:#cccccc;
	}
	
	.loadingDiv{
		background:#cccccc;
		opacity:0.4
	}
	
	.ui-widget-header {
		background: url("{{url:base}}media/jquery-ui/css/custom-theme/images/ui-bg_highlight-soft_75_cccccc_1x100.png") repeat-x scroll 50% 50% #CCCCCC !important;
		border: 1px solid #AAAAAA !important;
		color: #222222 !important;
		font-weight: bold !important;
	}
	.ui-widget {
	   font-size: 0.8em !important;
	}
	
	.read,.unread,.delete{
		width:25px;height:25px;display:block;
		background:url("{{url:base}}media/img/read.png") center center no-repeat;
	}
	.unread{
		background:url("{{url:base}}media/img/unread.png") center center no-repeat;
	}	
	.delete{
		background:url("{{url:base}}media/img/delete.png") center center no-repeat;
	}
	
	.item-read-unread,
	.item-delete{
		float:left;
		margin-left:5px;
		z-index:100;
	}
	
	.item-date{margin-top:5px;margin-left:5px;float:left;}
	
	span.unread_number,span.unread-thread{display:inline!important;}
	span.unread-thread{font-size:10px;}
	
	.item-number-unread{
		position:absolute;
		top:5px;right:10px;
		font-size: 10px;
	}
	
	.unread_conversation{background:#629632 !important;}
	
	#browse_threads{margin-top:5px;}
	
	.thread_conversation{
		background:#cfcfcf;
		margin-top:10px;
	}
	
	.thread_conversation .item-name,
	.thread_conversation .item-message{margin-left:20px;}
</style>
 
<div id="content-left">
	<div id="personal-tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
		<div id="tabs-1" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
			<!-- content -->	
			<div class="dashboard-info-wrapper">
				<div class="dashboard-info-left">
					<a href="javascript:void(0);" onclick="return open_question_dialog(<?php echo $USER->getId();?>);"><?php echo language_translate('support_label_new_message');?></a>
					<div class="clear"></div>
					<a href="<?php echo site_url_lang("agent/account/messages");?>"><?php echo language_translate('support_label_see_all_messages');?> (<span class="unread_number"><?php echo $countUnread;?></span> <?php echo language_translate('right_nav_unread_message');?>)</a>
				</div>
				<div class="dashboard-info-right">
					<div id="message-thread">
						<?php 
							 $this->load->view('agent/account/async_loadAllThreads');
						?> 
					</div>
				</div>
				<div class="clear"></div>
			</div>
			
			
			<!-- end content -->
		</div>
	</div>
</div>
 

<?php $this->load->view('agent/right_nav'); ?>



<script type="text/javascript">
	//loadAllThreads();
	
	$(document).ready(function() { 
		$('.read-thread-item').live('click',function(){
			$id = $(this).attr('rel');
			loadMessageOfThread($id);
		});
		
		$('.mark-item').live('click',function(){
			$id = $(this).attr('rel');
			toggleReadUnreadMessage($id);
			return false;
		});
		
		$('.delete').live('click',function(){
			$id = $(this).attr('rel');
			if($('.delete[rel="'+$id+'"]').hasClass('is_thread')){
				$mode = 'THREAD';
			}else{
				$mode = 'MESSAGE';
			}
			deleteMessage($id,$mode);
			return false;
		});
		
		var options = { 
			dataType:  'json', 		
			beforeSubmit:  validateB4SubmitSendMessage,  
			success:       processAfterRespondingSendMessage   
		};
		$("#submit_send_message").live("submit",function(e){
			e.preventDefault(); // <-- important
			$(this).ajaxSubmit(options);
			return false;
		});	
		
		$("#submit_send_message_reply_thread").live("submit",function(e){
			e.preventDefault(); // <-- important
			$(this).ajaxSubmit(options);
			return false;
		});	
		
		$('#browse_threads .pagination a').live('click',function(){
			$link = $(this).attr('href');
			$('#message-thread').addClass('loadingDiv');
			$.get($link,{},function(res){$('#message-thread').removeClass('loadingDiv');$('#message-thread').html(res);});
			return false;
		});
	});	
	
	function toggleReadUnreadMessage(message_id){
		$('#message-thread').addClass('loadingDiv');
		$.post(BASE_URI+'agent/async/toggleReadUnreadMessage',{message_id:message_id},function(res){
			$('.mark-item[rel="'+message_id+'"]').removeClass('read').removeClass('unread').addClass(res.css);
			$('.unread_number').text(res.unreadMsg);
			$('#message-thread').removeClass('loadingDiv');
			//if(MODE == 'thread'){
				$thread_id = res.thread_id;
				$('#unread_message_item_'+$thread_id).html(res.unreadMsgThread);
				if(res.css == 'read'){
					$('.message_item_'+message_id).removeClass('unread_conversation');
				}else{
					$('.message_item_'+message_id).addClass('unread_conversation');
				}
			//}
		},'json');
		
	}
	
	function deleteMessage(message_id,mode){
		$('#message-thread').addClass('loadingDiv');
		$.post(BASE_URI+'agent/async/deleteMessage',{message_id:message_id},function(res){
			$('.unread_number').text(res.unreadMsg);
			if(mode != 'THREAD'){
				$('.thread-item[rel="'+message_id+'"]').fadeOut();
				$('#message-thread').removeClass('loadingDiv');
				$('#unread_message_item_'+res.thread_id).html(res.unreadMsgThread);
			}else{
				loadAllThreads();
			}
		},'json');
	}
	
	function loadAllThreads(){
		$('#message-thread').addClass('loadingDiv');
		$.get(BASE_URI+'agent/async/loadAllThreads',{},function(res){
			$('#message-thread').html(res).removeClass('loadingDiv');
			$('.unread_number').text(UNREAD);
		});
	}
	
	function loadMessageOfThread($thread_id){
		$('#message-thread').addClass('loadingDiv');
		$.get(BASE_URI+'agent/async/loadMessageOfThread',{thread_id:$thread_id},function(res){
			//$('#message-thread').html(res).removeClass('loadingDiv');
			$('#conversation_'+$thread_id+' .thread_conversation').html(res);
			$('#message-thread').removeClass('loadingDiv');
			$('.unread_number').text(UNREAD);
			$('#unread_thread_'+$thread_id).text(UNREAD_THREAD);
			$('.delete_thread_'+$thread_id).hide();
		});
	}

	function closeMessageOfThread($thread_id){
		$('#conversation_'+$thread_id+' .thread_conversation').html('');
		$('.delete_thread_'+$thread_id).show();
	}
	
	function replyMessageOfThread(thread_id,username){
		$('#greyDiv').removeClass('hidden');
		$.post(BASE_URI+'agent/async/loadReplyThreadDialog',{thread_id:thread_id,username:username},function(res){
			$('#hiddenElement').html(res);
			$('#hiddenElement').dialog(
				{
					 width: 550,
					 height:350 ,
					 draggable: false,
					 beforeClose: function(event, ui) { 
						$('#greyDiv').addClass('hidden');
						loadMessageOfThread(thread_id);
					 },
					 close: function(event, ui) { },
					title: '<?php echo language_translate('support_label_send_message_title');?>' 
				}
			);
			$('.ui-dialog').center();
		});
	}
	
	function open_question_dialog(user_id){
		$('#greyDiv').removeClass('hidden');
		$.post(BASE_URI+'agent/async/load_send_message_form',{user_id:user_id},function(res){
			$('#hiddenElement').html(res);
			$('#hiddenElement').dialog(
				{
					 width: 550,
					 height:350 ,
					 draggable: false,
					 beforeClose: function(event, ui) { 
						$('#greyDiv').addClass('hidden');
					 },
					 close: function(event, ui) { 
						if(MODE == 'thread'){
							loadAllThreads();
						}
					},
					title: '<?php echo language_translate('support_label_send_message_title');?>' 
				}
			);
			$('.ui-dialog').center();
		});
	}
	
	
	function validateB4SubmitSendMessage(formData, jqForm, options){
		$('.loading_update_image').show();
		return true;
	}

	function processAfterRespondingSendMessage(res, statusText, xhr, $form) {
		$('.loading_update_image').hide();	
		
		if(res.result == 'ok'){
			$('#wrap-delete-item').remove();
			updatedShow('message-status');
			$('.unread_number').text(res.unreadMsg);
			$('#unread_message_item_'+res.thread_id).html(res.unreadMsgThread);
			$('#hiddenElement').dialog('close');
		}else{
			debug(responseText);
		}
	}
</script>